/**
 * 
 */
var _url = "service.do?s=statistics";
var galleries;

$(function(){
	init(0);
	galleries = $('.ad-gallery').adGallery();
	galleries[0].settings.effect = "slide-vert"; // slide-hori,slide-vert,resize,fade
	galleries[0].slideshow.toggle();
	galleries[0].settings.description_wrapper = $("#descriptions");
    $('#toggle-description').click(
      function() {
        if(!galleries[0].settings.description_wrapper) {
          galleries[0].settings.description_wrapper = $('#descriptions');
        } else {
          galleries[0].settings.description_wrapper = false;
        }
        return false;
      }
    );
	
	// 标签切换
	$("#tabMenu li").each(function(index) {
        $(this).click(function() {
            $("#tabMenu li.tabMenuItemFocus").removeClass("tabMenuItemFocus").addClass("tabMenuItem");
            $(this).removeClass("tabMenuItem").addClass("tabMenuItemFocus");
            $("#tabContent li.item:eq(" + index + ")").fadeIn()
            .siblings().fadeOut();
			refreshData(index);
        });
    });
	new refreshData("0");
});

/**
 * 初始化
 * @param index 默认显示第index+1个
 * @returns
 */
var init = function(index) {
	$("#tabMenu li:eq(" + index + ")").removeClass("tabMenuItem").addClass("tabMenuItemFocus");
	$("#tabContent li:eq(" + index + ")").show().siblings().hide();
}

/**
 * 刷新数据
 * @param {String} reqList 需要刷新的分页的索引集合 e.g.: "1,4,5"
 * @param {Boolean} manualRefresh=true 是否手动刷新
 */
var refreshData = function(reqList) {
	var manualRefresh = arguments[1] || false;
	$.post(_url, {op:"refreshData","reqList":reqList}, function(data){
		var event = eval("(" + data + ")");
		if(event.result && event.result.length == 0) {
			alert("无数据");
			return;
		}
		if(event.returnCode == "success") {
			fillData(event.result);
		} else if(event.errorMessage != null && event.errorMessage != "" && !manualRefresh) {
			alert(event.errorMessage);
		} else if(!manualRefresh) {
			alert("获取失败");
		}
	}, "html");
}

/**
 * 填充数据
 * @param {Object} dataList 数据集
 */
var fillData = function(dataList) {
	$.each(dataList, function(index, data) {
		if(data.sType == 0) {
			fillMediaData(data);
		} else if(data.sType == 1) {
			fillAdData(data);
		}
	});
}

var fillMediaData = function(data) {
	var chartDataHtml = "";
	var buildPicLi = function(conf){
		var pHtml = 
				"<li>" +
				"  <a href='" + conf.href + "'>" +
				"    <img src='" + conf.src + "'" +
				"         title='" + conf.title + "'" +
				"         alt='" + conf.alt + "'" +
				"         class='" + conf.className + "'" +
				"    />" +
				"  </a>" +
				"</li>";
		return pHtml;
	};
	var buildAlt = function(data) {
		var altHtml = 
			"<table id=\"tbData\" class=\"tbData\">" +
			  "<tr>" +
			    "<td class=\"tbHead\">媒体</td>" +
			    "<td class=\"tbHead\">点击数</td>" +
			  "</tr>";
		$.each(data, function(index, obj){
			if(index%2 == 1) {
				altHtml = altHtml +
				"<tr>" +
		        "<td class=\"tbContent1\">" + obj.name + "</td>" +
		        "<td class=\"tbContent1\">" + obj.count + "</td>" +
		      	"</tr>"
			} else {
				altHtml = altHtml +
				"<tr>" +
		        "<td class=\"tbContent2\">" + obj.name + "</td>" +
		        "<td class=\"tbContent2\">" + obj.count + "</td>" +
		      	"</tr>"
			}
		});
		altHtml = altHtml + "</table>";
		return altHtml;
	};
	chartDataHtml = chartDataHtml + buildPicLi({
		href:data.cData.chartTempPath,
		src:data.cData.chartTempPath,
		title:"",//data.cData.chartName,
		alt:buildAlt(data.tData),
		className:"thumbPic"
	});
	$.each(data.cDataList, function(index, obj){
		chartDataHtml = chartDataHtml + buildPicLi({
			href:obj.chartTempPath,
			src:obj.chartTempPath,
			title:"",//obj.chartName,
			alt:buildAlt(data.adList[index]),
			className:"thumbPic"
		});
	});
	$(".ad-thumb-list").html(chartDataHtml);
	$("#descriptions").html("");
	$(".ad-controls").html("");
	galleries = $('.ad-gallery').adGallery();
	galleries[0].settings.effect = "slide-vert"; // slide-hori,slide-vert,resize,fade
	galleries[0].slideshow.toggle();
	galleries[0].settings.description_wrapper = $("#descriptions");
}

var fillAdData = function(data) {
	// TODO
}
